package com.level2app.di.retrofit;

import com.level2app.bean.AddressListBean;
import com.level2app.bean.CheckReferralCodeBean;
import com.level2app.bean.MessageBean;
import com.level2app.utils.GsonUtils;

import java.util.Map;

import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class RetrofitHelper {
    private final ApiConstants mApiConstants;

    public RetrofitHelper(ApiConstants paramApiConstants) {
        this.mApiConstants = paramApiConstants;
    }

    public static <T> FlowableTransformer<T, T> appointThread() {
        return upstream -> upstream.subscribeOn(Schedulers.io())
                // 使用observerOn()指定订阅者运行的线程
                .observeOn(AndroidSchedulers.mainThread());
    }

    public static RequestBody handlerRequestData(Map<String, Object> map) {
        try {
//            map.put("userId", CoreManager.getUserId());
//            map.put("appVersion", ApiConstants.APP_VERSION);
//            map.put("reqTime", TimeUtils.getSystemTime());
//            map.put("loginType", AppUtils.getAppName());
//            map.put("appVersionReal", AppUtils.getAppVersionName());
//            map.put("phoneType", ApiConstants.PHONE_TYPE);
            return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), GsonUtils.toJson(map));
        } catch (Exception e) {
            return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "");
        }
    }


    public Flowable<MessageBean> sendLoginMail(Map<String, Object> data) {
        return this.mApiConstants.sendLoginMail(handlerRequestData(data)).compose(appointThread());
    }

    public Flowable<MessageBean> sendRegisterMail(Map<String, Object> data) {
        return this.mApiConstants.sendRegisterMail(handlerRequestData(data)).compose(appointThread());
    }

//    public Flowable<BaseModel<String>> getAddress(Map<String, Object> data) {
//        return this.mApiConstants.getAddress(handlerRequestData(data)).compose(appointThread());
//    }

    public Flowable<AddressListBean> getAddress() {
        return this.mApiConstants.getAddress("10", "10").compose(appointThread());
    }

    public Flowable<CheckReferralCodeBean> checkReferralCode(Map<String, Object> data) {
        return this.mApiConstants.checkReferralCode(handlerRequestData(data)).compose(appointThread());
    }
}
